package handler

import (
	repository "antiquity_map/internal/repositories"
	"net/http"

	"github.com/gin-gonic/gin"
)

func (h *Handler) Filter(c *gin.Context) {
	type CreateTime struct {
		EndTime   string `json:"endTime"`
		StartTime string `json:"startTime"`
	}

	type Location struct {
		Attitude float64 `json:"attitude"`
		Latitude float64 `json:"latitude"`
	}

	type UpdateTime struct {
		EndTime   string `json:"endTime"`
		StartTime string `json:"startTime"`
	}
	type Request struct {
		CreateTime *CreateTime `json:"createTime,omitempty"`
		Location   *Location   `json:"location,omitempty"`
		Name       *string     `json:"name,omitempty"`
		UpdateTime *UpdateTime `json:"updateTime,omitempty"`
	}
	var request repository.Request
	err := c.BindJSON(&request)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	}
	page := c.Params.ByName("page")
	pageSize := c.Params.ByName("pageSize")
	onlyBriefInfo := c.Params.ByName("onlyBriefInfo")
	type LocationInfo struct {
		CreaterCredit float64 `json:"createrCredit"`
		CreaterID     string  `json:"createrID"`
		Latitude      float64 `json:"latitude"`
		LocationID    float64 `json:"locationID"`
		LocationIntro string  `json:"locationIntro"`
		LocationName  string  `json:"locationName"`
		Longitude     float64 `json:"longitude"`
	}

	type Record struct {
		CreaterCredit float64 `json:"createrCredit"`
		CreaterID     string  `json:"createrID"`
		RecordID      string  `json:"recordID"`
		Thumbnail     string  `json:"thumbnail"`
		UploadTime    string  `json:"uploadTime"`
	}
	type Result struct {
		LocationInfo []LocationInfo `json:"locationInfo"`
		Records      []Record       `json:"records,omitempty"`
	}

	type RequestBody struct {
		Name         string `json:"name" binding:"required"`
		Introduction string `json:"introduction" binding:"required"`
		Location     struct {
			Longitude float64 `json:"longitude" binding:"required"`
			Latitude  float64 `json:"latitude" binding:"required"`
		} `json:"location" binding:"required"`
	}

	result, err := h.locationService.Filter(page, pageSize, onlyBriefInfo, request)
	// Implement filter logic here
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, result)

}

func (h *Handler) GetLocationDetail(c *gin.Context) {
	locationID := c.Param("location_id")

	// 如果 locationID 为空，返回错误
	if locationID == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "location_id is required"})
		return
	}
	var location repository.Request_LocationDetail
	location, err := h.locationService.GetLocationDetail(locationID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, location)
}

func (h *Handler) DeleteLocation(c *gin.Context) {
	// 从路由中获取 location_id
	locationID := c.Param("location_id")

	// 如果 locationID 为空，返回错误
	if locationID == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "location_id is required"})
		return
	}
	err := h.locationService.DeleteLocation(locationID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "Location deleted successfully"})
}

func (h *Handler) CreateLocation(c *gin.Context) {
	// Implement create location logic here
	var requestBody repository.RequestBody
	err := c.BindJSON(&requestBody)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	err = h.locationService.CreateLocation(requestBody.Name, requestBody.Introduction, requestBody.Location.Latitude, requestBody.Location.Longitude)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "Location created successfully"})

}
